package com.wodsy.comtroller;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wodsy.bean.User;
import com.wodsy.service.TestService;

@Controller
public class HomeController {

	@Autowired
	private TestService testService;

	@RequestMapping(value = { "/home","/index","/" }, method = RequestMethod.GET)
	public String showHomePage(Map<String, Object> model) {
		// model.put("testName",testService.getName());
		System.out.println("home log !");
		return "home";
	}
	
	@RequestMapping(value = { "/hi" }, method = RequestMethod.GET)
	public String sayHi(@RequestParam("userName")String name,Model model) {
		model.addAttribute("msg", testService.getMessage(name));
		return "home";
	}
	
	@RequestMapping(value = {"/account"},method = RequestMethod.GET,params = {"new"})
	public String toCreate(Model model){
		model.addAttribute(new User());
		return "edit";
	}
	
	@RequestMapping(value = {"/account"},method = RequestMethod.POST)
	public String create(@Valid User user,BindingResult bindingResult){
		if(bindingResult.hasErrors()){
			return "edit";
		}
		System.out.println("保存user:"+user.getFullName());
		return "redirect:/account/"+user.getUsername();
	}

	@RequestMapping(value = {"/account/{userName}"},method = RequestMethod.GET)
	public String show(@PathVariable String userName,Model model){
		User user = new User();
		user.setFullName(userName);//应该是查询一个user
		model.addAttribute(user);

		return "view";
	}

}
